package com.itsq.service.resources.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsq.common.bean.ErrorEnum;
import com.itsq.common.constant.APIException;
import com.itsq.pojo.dto.PageUsersDto;
import com.itsq.pojo.entity.Personnel;
import com.itsq.pojo.entity.Users;
import com.itsq.mapper.UsersMapper;
import com.itsq.service.resources.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-02-01
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired(required = false)
    private UsersMapper usersMapper;

    @Override
    public Page<Users> selUsersPageFront(PageUsersDto pageUsersDto) {
        Page<Users> page=new Page<>(pageUsersDto.getPageIndex(),pageUsersDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageUsersDto.getOrderByField()==null){
            queryWrapper.orderByDesc("cre_date");
        }else{
            queryWrapper.orderByDesc(pageUsersDto.getOrderByField());
        }
        return usersMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Page<Users> selUsersPageAfter(PageUsersDto pageUsersDto) {
        Page<Users> page=new Page<>(pageUsersDto.getPageIndex(),pageUsersDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageUsersDto.getOrderByField()==null){
            queryWrapper.orderByDesc("cre_date");
        }else{
            queryWrapper.orderByDesc(pageUsersDto.getOrderByField());
        }
        return usersMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Integer insert(Users users) {
        int i = usersMapper.insert(users);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }
        return i;
    }

    @Override
    public Integer update(Users users) {
        int i = usersMapper.updateById(users);
        if(i<1){
            throw new APIException(ErrorEnum.is_ISNULL);
        }
        return i;
    }

    @Override
    public Users selUsersById(Integer id) {
        return usersMapper.selectById(id);
    }

    @Override
    public List<Users> selUsersList() {
        QueryWrapper queryWrapper=new QueryWrapper();
        return usersMapper.selectList(queryWrapper);
    }

    @Override
    public List<Users> selUsersStateList() {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("state",2);
        return usersMapper.selectList(queryWrapper);
    }
}
